﻿using System;
using System.Collections.Generic;
using System.Linq;
using chatGUI.DataBase.Entity;
using chatGUI.DataBase.Helper;
using chatGUI.Model;
using chatGUI.SingeltonData;

namespace chatGUI.DataBase.Dao;

public class GroupDao{
    private static readonly IFreeSql  fsql= SqlHelper.FSql;
    
    public static Group Save(Group group){
        return fsql.GetRepository<Group>().InsertOrUpdate(group);
    }

    public static int Insert(Group group){
        try {
            return fsql.Insert(group).ExecuteAffrows();
        }
        catch (Exception) {
            return 0;
        }
    }
    
    public static int InsertList(List<Group> groupList){
        try {
            return fsql.Insert(groupList).ExecuteAffrows();
        }
        catch (Exception) {
            return 0;
        }
    }

    public static int DeleteAll(){
        try {
            return fsql.Select<Group>().ToDelete().ExecuteAffrows();
        }
        catch (Exception) {
            return 0;
        }
    }

    
    public static int DeleteById(string id){
        try {
            return fsql.Select<Group>().Where(g => g.Gid.Equals(id)).ToDelete().ExecuteAffrows();
        }
        catch (Exception) {
            return 0;
        }
    }

    public static Group SelectLast(){
        return fsql.Select<Group>().ToList().Last();
    }

    public static Group SelectById(string id){
        return fsql.Select<Group>().Where(g => g.Gid.Equals(id)).ToOne();
    }

    public static List<Group> SelectByName(string name){
        return fsql.Select<Group>().Where(g=>g.Gname.Equals(name)).ToList();
    }

    public static List<Group> SelectLikeName(string key){
        return fsql.Select<Group>().Where(g=>g.Gname.Contains(key)).ToList();
    }
}